﻿using System;
using System.Collections.Generic;
using System.Data;
using LDataAccess;
using LDriver;
using LDriver.Protocols;
using LModels.Base;
using Models.Collections;
using Proxy = LModels.Proxy.Proxy;

namespace Models
{
    public class Recording : BaseModel
    {
        public long IDRecordings { get; set; }
        public DateTime Datetime { get; set; }
        public string Command { get; set; }
        private long IDWorkspace { get; set; }

        private Workspace workspace;
        [LazyObj]
        public Workspace RecordingWorkspace
        {
            get
            {
                if (workspace == null)
                {
                    workspace = new Factory<Workspace>(px).Load(IDWorkspace) as Workspace;
                    return workspace;
                }

                return null;
            }
            
        }

        public SurveysCollection RecordingSurveys{ get; set; }
        
        
        private ICommunicationProvider Provider { get; set; }
        private ICommunicationProtocol Protocol { get; set; }

        
        public Recording()
        {
        }

        public Recording(Proxy pxProxy) : base(pxProxy)
        {
        }


        protected internal override BaseModel Load(long idRecording)
        {
            IDataReader dr = base.px.Recording.Load(idRecording);
            dr.Read();

            SetPK(dr["IDRecordings"] == DBNull.Value ? -1 : (long)dr["IDRecordings"]);
            Datetime = (DateTime)dr["Date"];
            Command = dr["Command"].ToString();
            IDWorkspace = dr["IDWorkspace"] == DBNull.Value ? -1 : (long) dr["IDWorkspace"];
            
            return this;

        }

        public void Save()
        {
            base.px.Recording.Save(this);
        }

        public void SaveSurvey()
        {
            // Bulk insert
            base.px.Survey.SaveBulk(this.IDRecordings, RecordingSurveys);

            // Cycle insert
            //foreach (Survey recordingSurvey in RecordingSurveys)
            //{
            //    base.px.Survey.Save(this, recordingSurvey.survey);
            //}
        }

        public void SaveSurvey(LidarSurvey s)
        {
            base.px.Survey.Save(this, s);
        }


    }

    public class LazyObjAttribute : Attribute
    {

    }

    namespace Collections
    {
        public class RecordingCollection : BaseCollection
        {
            private RecordingCollection(){}
            public RecordingCollection(Proxy pxProxy) : base(pxProxy) { }

            public RecordingCollection ListById(long IDRecording)
            {
                RecordingCollection ret = new RecordingCollection();
                IDataReader dr = px.Recording.ListById(IDRecording);

                while (dr.Read())
                    ret.Add(new Factory<Recording>(px).Load((long) dr["IDRecordings"]) as Recording);

                return ret;
            }

            public RecordingCollection List()
            {
                RecordingCollection ret = new RecordingCollection();
                IDataReader dr = px.Recording.List();

                while (dr.Read())
                    ret.Add(new Factory<Recording>(px).Load((long)dr["IDRecordings"]) as Recording);

                return ret;
            }

            public bool Save()
            {
                try
                {
                    foreach (Recording rec in this)
                        rec.Save();

                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                
            }

            public void SaveAndAddSurveys(List<LidarSurvey> surveys)
            {
                //TODO: Implementare Salvataggio Recording e successive Survey

            }
        }

    }

}
